package com.app.twth.ui.finalprocess.lpn;


import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityFinalprocessLpnBinding;
import com.app.twth.ui.finalprocess.FinalProcess;
import com.app.twth.ui.finalprocess.FinalProcessViewModel;
import com.app.twth.ui.finalprocess.trk.FinalProcessTrkActivity;
import com.app.twth.utils.DividerItemDecoration;
import com.app.twth.utils.EditTextUtil;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

import java.util.ArrayList;

/**
 * @describe :
 */
public class FinalProcessLpnActivity extends BaseActivity<ActivityFinalprocessLpnBinding, FinalProcessViewModel> {
    private String TAG = getClass().getSimpleName();
    private String batchNo;
    private FinalProcessLpnAdapter adapter;
    private ArrayList<String> tallyList = new ArrayList<>();

    @Override
    protected void initListener() {
        viewModel.finalProcessLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("FinalProcess", data);
            AppRouter.showActivityForResult(mContext, FinalProcessTrkActivity.class, bundle, Constant.ForResult2);
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "FinalProcess(LPN)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext("");
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        batchNo = bundle.getString("batchNo");
        tallyList = bundle.getStringArrayList("list");

        binding.tvBatchNo.setText(batchNo);
        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);

        adapter = new FinalProcessLpnAdapter(this, tallyList, position -> {
            if (tallyList.size() <= position) {
                return;
            }
            barcodeStr = tallyList.get(position);
            viewModel.returnFinalProcessItem(batchNo, barcodeStr);

        });
        binding.recyclerview.setHasFixedSize(true);
        binding.recyclerview.setItemAnimator(new DefaultItemAnimator());

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerview.setLayoutManager(layoutManager1);
        //添加装饰类
        binding.recyclerview.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
        binding.recyclerview.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                binding.tvCode.setText("");
                break;
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
                    return;
                }
                timeMillis = System.currentTimeMillis();
                barcodeStr = binding.tvCode.getText().toString();
                if (Utils.isEmpty(barcodeStr)) {
                    ToastUtil.showToast("Please enter container no.");
                    return;
                }
                viewModel.returnFinalProcessItem(batchNo, barcodeStr);
                break;
        }
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                EditTextUtil.hideInput(mContext);

                if (Utils.isEmpty(barcodeStr)) {
                    return false;
                }
                viewModel.returnFinalProcessItem(batchNo, barcodeStr);
            }
            return false;
        }
    };

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
        viewModel.returnFinalProcessItem(batchNo, barcodeStr);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ForResult2 && resultCode == RESULT_OK) {
            FinalProcess finalProcessReturn = (FinalProcess) data.getSerializableExtra("FinalProcessReturn");
            if (finalProcessReturn == null) {
                Intent intent = getIntent();
                intent.putExtra("FinalProcessReturn", finalProcessReturn);
                setResult(RESULT_OK, intent);
                finish();
                return;
            }
            ArrayList<String> lpns = finalProcessReturn.getIbLpnList();
            if (lpns == null) {
                Intent intent = getIntent();
                intent.putExtra("FinalProcessReturn", finalProcessReturn);
                setResult(RESULT_OK, intent);
                finish();
                return;
            }
            if (lpns.size() == 0) {
                Intent intent = getIntent();
                setResult(RESULT_OK, intent);
                finish();
                return;
            }

            binding.tvCode.setText("");
            tallyList = lpns;
            adapter.setData(tallyList, 1);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
